import React, { useState, useEffect } from 'react';
import { AppstoreOutlined, MailOutlined, SettingOutlined, LogoutOutlined } from '@ant-design/icons';
import { Menu, Layout, Modal, message } from 'antd';
import './index.less';
import { Outlet, useNavigate } from 'react-router-dom';
import { request } from '@apis/request';

function getItem(label, key, icon, children, type) {
  return {
    key,
    icon,
    children,
    label,
    type,
  };
}

const items = [
  getItem('messageBoard', '/message-board', <MailOutlined />, [
    getItem('往期功能', '/previous-fonc'),
    getItem('正在研发', '/under-development'),
    getItem('留言审核', '/auditing-leave'),
    getItem('Option 4', '4'),
  ]),
  getItem('Navigation Two', 'sub2', <AppstoreOutlined />, [
    getItem('Option 5', '5'),
    getItem('Option 6', '6'),
    getItem('Submenu', 'sub3', null, [getItem('Option 7', '7'), getItem('Option 8', '8')]),
  ]),
  getItem('Navigation Three', 'sub4', <SettingOutlined />, [
    getItem('Option 9', '9'),
    getItem('Option 10', '10'),
    getItem('Option 11', '11'),
    getItem('Option 12', '12'),
  ]),
  getItem('退出登录', 'logout', <LogoutOutlined />)
]; // submenu keys of first level

const rootSubmenuKeys = ['/message-board', 'sub2', 'sub4'];

const { Content } = Layout;

const Main = () => {

  const navigate = useNavigate();

  const [openKeys, setOpenKeys] = useState(['/message-board']);
  const [isModalOpen, setIsModalOpen] = useState(false);

  const onOpenChange = (keys) => {
    const latestOpenKey = keys.find((key) => openKeys.indexOf(key) === -1);

    if (rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
      setOpenKeys(keys);
    } else {
      setOpenKeys(latestOpenKey ? [latestOpenKey] : []);
    }
  };

  const handleOk = async () => {
    if (localStorage.getItem('userInfo')) {
      await request('/logout', {}, 'post');
      message.success('已退出登录');
      setIsModalOpen(false);
      navigate('/login', { replace: true });
      localStorage.removeItem('userInfo');
    }
  };
  const handleCancel = () => {
    setIsModalOpen(false);
  };

  useEffect(() => {
    const userInfo = localStorage.getItem('userInfo');
    if (!userInfo) {
      navigate('/login', { replace: true });
    }
  }, [navigate])

  return <div className="main">
    <Menu
      mode="inline"
      theme="dark"
      openKeys={openKeys}
      onOpenChange={onOpenChange}
      style={{
        width: 256,
      }}
      items={items}
      onClick={({ key, keyPath, domEvent }) => {
        // console.log('key: ', key);
        // console.log('keyPath: ', keyPath);
        if (key === 'logout') {
          setIsModalOpen(true);
        } else {
          const url = keyPath.reduce((a, b) => b + a);
          navigate(url);
        }
      }}
    />
    <Layout>
      <Content>
        <Outlet />
      </Content>
    </Layout>
    <Modal
      title="你确定要退出登录吗？"
      cancelText="取消"
      okText="确定"
      open={isModalOpen}
      onOk={handleOk}
      onCancel={handleCancel}
    >
    </Modal>
  </div>
}

export default Main;